package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import pojo.Book;
import util.DBUtil;
import dao.BookDao;

public class BookDaoImpl implements BookDao {

	Connection conn;
	PreparedStatement ps;

	@Override
	public int addBook(Book book) {
		conn = getConnection();
		String sql = "insert into books(bid, bname, img) values(?,?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ps.setString(1, (book.getBid() != null) ? book.getBid() : UUID
					.randomUUID().toString());
			ps.setString(2, book.getBname());
			ps.setString(3, book.getImg());

			int m = ps.executeUpdate();
			conn.commit();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public int updateBook(Book book) {
		conn = getConnection();
		String sql = "update books set bname=?,img=? where bid=?";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ps.setString(1, book.getBname());
			ps.setString(2, book.getImg());
			ps.setString(3, book.getBid());
			int m = ps.executeUpdate();
			conn.commit();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
		}
	}

	@Override
	public List<Book> findAllBooks() {
		conn = getConnection();
		List<Book> list = new ArrayList<Book>();
		String sql = "select * from books";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			ResultSet set = ps.executeQuery();
			while (set.next()) {
				Book book = new Book();
				book.setBid(set.getString("bid"));
				book.setBname(set.getString("bname"));
				book.setImg(set.getString("img"));
				list.add(book);
			}
			conn.commit();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return list;
		}
	}

	@Override
	public Book searchBook(String bid) {
		conn = getConnection();
		
		String sql = "select * from books where bid=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, bid);
			ResultSet set = ps.executeQuery();
			if (set.next()) {
				Book book = new Book();
				book.setBid(set.getString("bid"));
				book.setBname(set.getString("bname"));
				book.setImg(set.getString("img"));
				return book;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();

			return null;
		}
	}

	@Override
	public int deleteBook(String bid) {
		conn = getConnection();
		String sql = "delete books where bid=?";
		try {
			ps = conn.prepareStatement(sql);

			ps.setString(1,bid);
			int m = ps.executeUpdate();
			return m;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}

	private Connection getConnection() {
		try {
			InitialContext cxt = new InitialContext();// / JNDI 树
			DataSource ds = (DataSource) cxt
					.lookup("java:/comp/env/jdbc/myoracle");// // 查找 [tomcat
															// 协议]/jdbc/xxxx
			Connection conn = ds.getConnection();
			// System.out.println(conn);
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		return DBUtil.createConnection();
	}
}
